package net.suncaper.demo.controller;

import net.suncaper.demo.domain.Admin;
import net.suncaper.demo.domain.Order;
import net.suncaper.demo.service.AdminService;
import net.suncaper.demo.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/order")
public class OrderController {
        @Autowired
        private OrderService orderService;

        @GetMapping("/add")
        public String OrderPage(Model model){
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            String date1=format.format(new Date());
            Calendar c1=Calendar.getInstance();
            c1.setTime(new Date());
            c1.add(Calendar.DAY_OF_MONTH,1);
            String date2=format.format(c1.getTime());
            model.addAttribute("date1",date1);
            model.addAttribute("date2",date2);
            model.addAttribute("order",new Order());
            return "/pages/order/add_order.html"; }

        @GetMapping("edit")
        public String editOrder(long orderid,Model model){
            model.addAttribute("order", orderService.findUserById(orderid));
            return "/pages/order/update_order.html";
        }

        @GetMapping("/search")
        public String searchOrder(Model model){
            model.addAttribute("order", new Order());
            return "/pages/order/search_order.html";
        }

        @GetMapping("detail")
        public String orderDetail(long orderid,Model model){
            model.addAttribute("order", orderService.findUserById(orderid));
            return "/pages/order/order_detail.html";
        }

        @PostMapping("/search")
        public  String selectOrder(Model model,Order order) {
            List<Order> orders=orderService.selectOrder(order);
            model.addAttribute("orders",orders);
            return "pages/order/order_list";
        }

        @PostMapping("/edit")
        public  String updateOrder(Order order) {
            orderService.updateOrder(order);
            return "redirect:/order/view";
        }

        @PostMapping("/add")
        public  String saveOrder(Order order) {
            Calendar calendar= Calendar.getInstance();
            Date date=calendar.getTime();
            order.setOrdertime(date);
            orderService.saveOrder(order);
            return "redirect:/order/view";
        }

        @GetMapping("/delete/{orderid}")
        public  String deleteOrderById(@PathVariable("orderid") long orderid){
            orderService.deleteOrderById(orderid);
            return "redirect:/order/view";
        }
        @InitBinder
        protected void init(HttpServletRequest request, ServletRequestDataBinder binder) {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            dateFormat.setLenient(false);
            binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
        }

        @GetMapping("/view")
        public  String Orderpage(Model model,
                                 @RequestParam(value="username",required = false) String username){
            List<Order> orders = orderService.findOrder(username);
            model.addAttribute("orders",orders) ;
            return "pages/order/order_list";
        }
    }
